package cn.kgc.bdqn_studentinfomanager.controller;

import cn.kgc.bdqn_studentinfomanager.pojo.common.ServerResponse;
import cn.kgc.bdqn_studentinfomanager.pojo.vo.FromData;
import cn.kgc.bdqn_studentinfomanager.service.UserService.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * by Shanshui CC on 2020/12/6 13:10
 */
@RestController
@RequestMapping("/api")

public class UserController {

    @Resource
    private UserService userService;

    /**
     * 登录
     *
     * @param phone
     * @param userPassword
     * @return
     */
    @RequestMapping(value = "/dologin", method = RequestMethod.GET)
    public ServerResponse doLogin(String phone, String userPassword, HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        return userService.queryTeacherInfo(phone, userPassword, request.getHeader("User-Agent"));
    }

    /**
     * 获取手机验证码
     *
     * @param phone
     */
    @RequestMapping("/getPhoneCode")
    public void get_PhoneCode(String phone, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        System.out.println("手机号为:" + phone);
        userService.get_PhoneCode(phone);
    }

//    /**
//     * 点击注册页面时加载专员下拉框
//     * @return
//     */
//    @RequestMapping("/showCommissioner")
//    public ServerResponse showCommissioner(){
//        return userService.showCommissioner();
//    }


    /**
     *  展示角色
     *  登录时选择角色的下拉框
     */
//    @RequestMapping("/role")
//    public ServerResponse role(){
//        return userService.role();
//    }

    /**
     * 注册
     *
     * @param fromData
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ServerResponse register(FromData fromData, HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin", "*");
        System.out.println("渠道专员手机号为:" + fromData.getCommissioner());
        System.out.println("手机验证码为:" + fromData.getPhoneCode());
        return userService.register(fromData);
    }


//    @RequestMapping("/retrieveByPhone")
//    public ServerResponse retrieveByPhone(String phone){
//        return userService.retrieveByPhone(phone,phoneCode);
//    }


    /**
     * 短信登录
     *
     * @param phone
     * @param phoneCode
     * @return
     */
    @RequestMapping("/phoneCodeLogin")
    public ServerResponse phoneCodeLogin(String phone, String phoneCode, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        return userService.phoneCodeLogin(phone, phoneCode);
    }


    /**
     * 修改密码
     *
     * @param userPassword
     * @param newUserPassword
     * @param request
     * @return
     */
    @RequestMapping("/updatePwd")
    public ServerResponse updatepwd(String userPassword, String newUserPassword,
                                    HttpServletRequest request) {
        return userService.updateTeacherInfoUserPassword(userPassword, newUserPassword,
                request.getHeader("token"));
    }


    /**
     * 忘记密码
     *
     * @param phone
     * @param phoneCode
     * @return
     */
    @RequestMapping("/forgetPwd")
    public ServerResponse forgetPwd(String phone, String phoneCode, String newPassword) {
        return userService.forgetPwd(phone, phoneCode, newPassword);
    }



}
